﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer.Admin.Copy.IndexMarkStruct;
using System.Transactions;
using DataAccessLayer.Properties;
using IMModel.Domain;

namespace DataAccessLayer.Admin.Copy.Aggregation
{
    public class CopyGradations
    {
        private ICopy gradationCopy = new GradationCopy();

        public void HierarchicalCopyGradation(int oldIDRelation, int? toIDParentRelation, int fromIDTerm, int toIDTerm)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    int newIDRelation = gradationCopy.Copy(oldIDRelation, toIDParentRelation, fromIDTerm, toIDTerm);

                    foreach (GradationTerm gr in db.GradationTerms.Where(w => w.IDParentGradationTerm == oldIDRelation))
                    {
                        HierarchicalCopyGradation(gr.IDGradationTerm, newIDRelation, fromIDTerm, toIDTerm);
                    }

                    transaction.Complete();
                }
            }
        }
    }
}
